﻿/*
 * aspark
 * Email:mwyso@126.com
 * http://quicksl.codeplex.com/
 */

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Threading;
using System.Reflection;

using System.Linq;
using System.Windows.Automation.Peers;

namespace QuickSL
{
    public class DispatcherQueue : AsyncQueue<DispatcherQueueItem>
    {
        /// <summary>
        /// waiting for delegate implement completed
        /// </summary>
        AutoResetEvent _delegateCompleteEvent;

        private DispatcherQueue()
        {
            _delegateCompleteEvent = new AutoResetEvent(false);
        }

        private static DispatcherQueue _current = null;
        /// <summary>
        /// Current DispatchQueue
        /// </summary>
        public static DispatcherQueue Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new DispatcherQueue();

                }

                return _current;
            }
        }

        /// <summary>
        /// Enqueue a action
        /// </summary>
        /// <param name="action"></param>
        public void Enqueue(Action action)
        {
            DispatcherQueueItem asyncItem = new DispatcherQueueItem(action);
            base.Enqueue(asyncItem);
        }

        public void Enqueue(Action action, int index)
        {
            DispatcherQueueItem asyncItem = new DispatcherQueueItem(action);
            base.Enqueue(asyncItem, index);
        }

        public void Enqueue(UIElement target, string propertyName, object value)
        {
            Action action = null;

            if (target != null)
            {
                //property
                //dependencyProperty
                PropertyInfo property = target.GetType().GetProperty(propertyName);
                if (property != null)
                {
                    action = () =>
                    {
                        if (target != null)
                        {
                            //avoid the element has'nt exist on the UI
                            //if (FrameworkElementAutomationPeer.CreatePeerForElement(target).IsOffscreen())
                            //{ 
                            
                            //}
                            property.SetValue(target, value, null);
                        }
                    };
                }
                else//todo:attachedProperty
                { 
                    //target.getva
                    //Canvas.SetLeft
                }
            }

            this.Enqueue(action);
        }

        public override bool Remove(DispatcherQueueItem item)
        {
            //if the delegate is running
            if (item != _currentProcessingItem)
            {
                return base.Remove(item);
            }

            return false;
        }

        private DispatcherQueueItem _currentProcessingItem = null;

        /// <summary>
        /// process the delegate queue
        /// </summary>
        protected override void OnProcessing()
        {
            //if touch screen,pause the process
            TouchState.Current.WaitOne();

            _currentProcessingItem = base.Peek();

            if (_currentProcessingItem != null)
            {
                DispatcherOperation temp = Common.CurrentDispatcher.BeginInvoke(
                    () =>
                    {
                        try//the element does'nt exist on the UI
                        {
                            //invoke delegate
                            _currentProcessingItem.Operation();
                        }
                        catch{}

                        //process next delegate
                        _delegateCompleteEvent.Set();
                    }
                );

                _delegateCompleteEvent.WaitOne();//wating for delegate implement completed

                 base.Remove(_currentProcessingItem);
                _currentProcessingItem = null;
            }
        }
    }
}
